<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滑动发送请求</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <style>
        *{
            box-sizing: border-box;
        }
        #app{
            width: 500px;
            height: 700px;
            margin: 30px auto;
            box-shadow: 0 0 4px #333;
            overflow: scroll;
        }
        .box{
            width: 500px;
            height: 190px;
            border-bottom: 1px solid #cdcdcd;
            display: flex;
        }
        img{
            width: 200px;
            height: 160px;
        }
        button{
            position: fixed;
            top: 0;
        }
    </style>
</head>
<body>
    <div id="app"  ref="box">
        <div class="box" v-for="(item, index) in list" :key="index">
            <img :src="item.active" />
            <h4>{{ item.title }}</h4>
        </div>
        <button @click="show">切换</button>
    </div>
    <script>
        let show = new Vue({
            el:'#app',
            data:{
                list:[],
                page:1,
            },
            created(){
                this.show();
                console.log( document.body.scrollTop  );
                console.log(document.documentElement.scrollHeight);
                console.log(document.documentElement.offsetHeight);
            },
            mounted () {
                let box = this.$refs.box;
                box.onscroll = () => {
                    console.log('clientHeight',box.clientHeight)//可视区域高度
                    console.log('scrollTop',box.scrollTop)//文档卷起高度
                    console.log('scrollHeight',box.scrollHeight)//文档总的高度
                    let clientHeight = box.clientHeight;
                    let scrollTop = box.scrollTop;
                    let scrollHeight = box.scrollHeight;
                    if(clientHeight + scrollTop >= scrollHeight) {
                        this.page++;
                        this.show();
                    }
                }
            },
            methods:{
                show(){
                    $.ajax({
                        url:'http://localhost:3000/show',
                        method:'POST',
                        data:{
                            page:this.page,
                            size:5
                        },
                        success:( res ) => {
                            if(this.page == 1){
                                this.list = res.data;
                            }else{
                                this.list = this.list.concat(res.data)//拼接愿数组
                                console.log(this.list)
                            }
                            // let result = this.list
                            // result.push(res.data)
                        },
                        errror:( err ) => {
                            console.log('err===>', err )
                        }
                    })
                    // this.page += 1;
                },
                
            }
        })

    </script>
</body>
</html>